import Taro, { Component } from '@tarojs/taro'
import { View , Text , Image} from '@tarojs/components';
import { baseImgSrc } from '../../util/base_url';
import { AtButton , AtInput , AtMessage  } from 'taro-ui';
import CountDownButton from "../../components/countDown/countDown";
import {  sendCode , getOpenId , wxLogin } from '../../util/api';
import { setToken  , setRole } from '../../util/globaldata';
import './login.scss';
export default class Login extends Component {
    constructor(){
        super(...arguments);
        this.state = {
            phone:"",
            code:"",
            timpTxt:"获取验证码",
            isAuth:false,
        }
    }
    componentDidShow(){
        var _this = this;
        Taro.getSetting({
            success(res){
                console.log(res)
                _this.setState({isAuth:res.authSetting['scope.userInfo'] ? true : false})
            }
        })
        this.setState({timpTxt:"获取验证码",phone:"",code:"",pwd:""})
        clearInterval(this.timp)
    }
    bindPhoneIpt(value){
        this.setState({phone:value});
        return value
    }
    bindCodeIpt(value){
        this.setState({code:value});
        return value
    }
    countDownButtonPressed(){
        if(!this.state.phone){
            return false
        }
        //调用组件方法 开始按钮倒计时
        this.countDownButton.startCountDown();
        sendCode({phone:this.state.phone}).then(res => {
            Taro.showToast({title:res.msg,icon:'none'})

        })
    };
    toXieyi(model){
        Taro.navigateTo({url:`/pages/loginXieyi/loginXieyi?model=${model}`}); //1是协议3 是政策
    }
    authWeixin(e){  //微信授权
        if (e.detail.userInfo) {
            //用户点击允许 
            this.weixinLogin()
        } else {
            //用户按了拒绝按钮
            Taro.showModal({
                title: '提示',
                content: '您点击了拒绝授权，部分功能将无法使用',
                showCancel: false,
                confirmText: '返回',
            });
        }
    }
    weixinLogin(){ //已经授权过直接登录
        var _this = this;
        Taro.showLoading({title:"获取您的基本信息"});
        Taro.login({
            success: function (res) {
                if (res.code) {
                    let code = res.code;
                    Taro.getUserInfo().then(res => {
                        let userInfo = res.userInfo; //用户信息
                        console.log(res)
                        _this.login(userInfo,code)
                    })
                }
            }
        })
    }
    login(userInfo,weiXincode){
        getOpenId({code:weiXincode}).then( res => {
            if(res.code == 0){
                let openId = res.data;
                wxLogin({wid:openId,}).then(res => {
                    console.log(res)
                    if(res.code == 0){ //登录成功
                        setToken(res.user.token);
                        Taro.showToast({title:res.msg});
                        setTimeout(() => {
                           Taro.navigateBack();
                        },700)
                    }else{ //绑定手机号
                        Taro.redirectTo({
                            url:`/pages/bindphone/bindphone?wid=${openId}&nickname=${userInfo.nickName}&headimgurl=${userInfo.avatarUrl}`
                        })
                    }
                })
            }
        })
    }
    render(){
        let { isAuth } = this.state;
        return ( 
            <View className='login'>
                <Image className='login-image' src={`${baseImgSrc}/logo.png`} />
                {
                    isAuth ? <AtButton className='login-btn' type='primary' onClick={this.weixinLogin.bind(this)}>微信一键登录</AtButton>
                    : <AtButton className='login-btn' type='primary' openType='getUserInfo' onGetUserInfo={this.authWeixin.bind(this)}>微信一键登录</AtButton>
                }
                <View className='xieyi fn-ctr'>
                    <Text className='txt1'>登录即代表同意</Text>
                    <Text className='txt2' onClick={this.toXieyi.bind(this,1)}>《用户使用协议》</Text>     
                    <Text className='txt1'>和</Text> 
                    <Text className='txt2' onClick={this.toXieyi.bind(this,3)}>《隐私政策》</Text>      
                </View>  
                <AtMessage />
            </View>
        )
    }
}